package viper.GUI;

import java.awt.BorderLayout;
import java.awt.CardLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import viper.main.StateManager;

/**
 * @author Kevin Sin
 * <p>This class is responsible for the GUI.  It interacts w/ StateManger only
 * to display the right info to the user and responds to user actions.  A CardLayout
 * is used to develop different panels and integrate them easily.
 */
public class ViperGUI extends JFrame{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	//Singleton structure.
	public final static ViperGUI INSTANCE = new ViperGUI();

	public static final String LOGGED_OUT_CARD = "LOGGED_OUT_CARD";
	public static final String LOGGED_IN_CARD = "LOGGED_IN_CARD";
	public static final String CALL_SENDER_SETUP_CARD = "CALL_SENDER_SETUP_CARD";
	public static final String IN_CALL_CARD = "IN_CALL_CARD";
	
	private JPanel cardPanel;
	private CardLayout cl;
	
	private LoggedInPanel loggedInPanel;
	private LoggedOutPanel loggedOutPanel;
	private InCallPanel inCallPanel;
	private CallSenderSetupPanel callSenderSetupPanel;

	private ViperGUI() {

		setTitle("VIPER");
		setSize(300, 150);
		cardPanel = new JPanel();

		cl = new CardLayout();
		cardPanel.setLayout(cl);

		loggedInPanel = new LoggedInPanel();
		loggedOutPanel = new LoggedOutPanel();
		inCallPanel = new InCallPanel();
		callSenderSetupPanel = new CallSenderSetupPanel();
		
		cardPanel.add(loggedInPanel, LOGGED_IN_CARD);
		cardPanel.add(loggedOutPanel, LOGGED_OUT_CARD);
		cardPanel.add(inCallPanel, IN_CALL_CARD);
		cardPanel.add(callSenderSetupPanel, CALL_SENDER_SETUP_CARD);

		getContentPane().add(cardPanel, BorderLayout.NORTH);
	}

	/**
	 * Sets the size and displays the window.
	 */
	public void initialize(){
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setSize(500, 450);
		setResizable(false);
		changeCard(LOGGED_OUT_CARD);
		setVisible(true);
	}
	
	/**
	 * @param cardName - which JPanel to display
	 * Changes the window title and updates each panel before showing that panel.
	 */
	public void changeCard(String cardName){
		StateManager.INSTANCE.updatePeers();
		if(cardName.equals(LOGGED_OUT_CARD))
			setTitle("VIPER - Logged Out");
		else if(cardName.equals(LOGGED_IN_CARD))
		{
			setTitle("VIPER - Logged In as " + StateManager.INSTANCE.getUsername());
			loggedInPanel.update();
		}
		else if(cardName.equals(CALL_SENDER_SETUP_CARD))
		{
			setTitle("VIPER - Call Setup to Talk to " + StateManager.INSTANCE.getCalleeArray()[1]);
			callSenderSetupPanel.update();
		}
		else if(cardName.equals(IN_CALL_CARD))
		{
			setTitle("VIPER - In Call");
			inCallPanel.update();
		}	
		cl.show(cardPanel, cardName);
	}
}
